From 1bef866c681b2d0a2d16512604a668bd70dd869b Mon Sep 17 00:00:00 2001 From: Ralf Horstmann Date: Mon, 27 Jan 2020 22:24:04 +0100 Subject: [PATCH] Convert mynav to Format sub-class (#472) * Convert mynav to Format sub-class While there, switch to gpsbabel::File, QTextStream, and introduce enum for line type. * Switch mynav to TextStream * Include cleanup in mynav * Correct includes, add link to spec for mynav format --- CMakeLists.txt | 1 + GPSBabel.pro | 1 + Makefile.in | 5 ++- mynav.cc | 120 +++++++++++++++++++------------------------------ mynav.h | 101 +++++++++++++++++++++++++++++++++++++++++ vecs.h | 4 +- 6 files changed, 153 insertions(+), 79 deletions(-) create mode 100644 mynav.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f2e2bb719..aea703b4b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -147,6 +147,7 @@ set(HEADERS legacyformat.h magellan.h mapsend.h + mynav.h navilink.h session.h shapelib/shapefil.h diff --git a/GPSBabel.pro b/GPSBabel.pro index 7a31a0231..db32960ad 100644 --- a/GPSBabel.pro +++ b/GPSBabel.pro @@ -135,6 +135,7 @@ HEADERS = \ legacyformat.h \ magellan.h \ mapsend.h \ + mynav.h \ navilink.h \ session.h \ shapelib/shapefil.h \ diff --git a/Makefile.in b/Makefile.in index a9b85af05..80d5ba452 100644 --- a/Makefile.in +++ b/Makefile.in @@ -831,7 +831,8 @@ mtk_logger.o: mtk_logger.cc defs.h config.h zlib/zlib.h zlib/zconf.h \ cet.h inifile.h gbfile.h session.h src/core/datetime.h \ src/core/optional.h gbser.h mynav.o: mynav.cc defs.h config.h zlib/zlib.h zlib/zconf.h cet.h \ - inifile.h gbfile.h session.h src/core/datetime.h src/core/optional.h + inifile.h gbfile.h session.h src/core/datetime.h src/core/optional.h \ + mynav.h format.h navicache.o: navicache.cc defs.h config.h zlib/zlib.h zlib/zconf.h cet.h \ inifile.h gbfile.h session.h src/core/datetime.h src/core/optional.h \ cet_util.h src/core/file.h @@ -1008,7 +1009,7 @@ vecs.o: vecs.cc defs.h config.h zlib/zlib.h zlib/zconf.h cet.h inifile.h \ gbfile.h session.h src/core/datetime.h src/core/optional.h vecs.h \ format.h gpx.h src/core/file.h src/core/xmlstreamwriter.h \ src/core/xmltag.h legacyformat.h gbversion.h src/core/logging.h xcsv.h \ - ggv_bin.h + ggv_bin.h mynav.h vidaone.o: vidaone.cc defs.h config.h zlib/zlib.h zlib/zconf.h cet.h \ inifile.h gbfile.h session.h src/core/datetime.h src/core/optional.h vitosmt.o: vitosmt.cc defs.h config.h zlib/zlib.h zlib/zconf.h cet.h \ diff --git a/mynav.cc b/mynav.cc index 7fabf9ceb..7e84ca20d 100644 --- a/mynav.cc +++ b/mynav.cc @@ -1,8 +1,11 @@ /* Handle MyNav TRC format .trc and .ftn files - Copyright (c) 2014 Ralf Horstmann - Copyright (C) 2014 Robert Lipe, robertlipe+source@gpsbabel.org + For information on the data format see + http://www.mynav.it/hwdoc/dev/TRC_Format_Spec.pdf + + Copyright (c) 2014-2020 Ralf Horstmann + Copyright (C) 2014-2020 Robert Lipe, robertlipe+source@gpsbabel.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -20,39 +23,23 @@ */ -#include "defs.h" +#include #include +#include +#include #include +#include + +#include + +#include "mynav.h" + +/*************************************************************************** + * local helper functions * + ***************************************************************************/ -#define MYNAME "mynav" - -enum field_e { - fld_type = 0, - fld_lon, - fld_lat, - fld_direction, - fld_speed, - fld_altitude, - fld_timestamp, - fld_duration, - fld_gps_valid, - fld_distance, - fld_ascent, - fld_cadence, - fld_heart_rate, - fld_id, - fld_total_duration, - fld_terminator -}; - -static route_head* mynav_track; -static gbfile* fin; - -//*************************************************************************** -// local helper functions -//*************************************************************************** -static void -mynav_rd_line(const QString& line) +void +MyNavFormat::read_line(const QString& line, route_head* track) { QStringList fields = line.split("|"); @@ -70,11 +57,11 @@ mynav_rd_line(const QString& line) // only type 1 and type 5 lines contain coordinates bool ok = false; - int val_type = fields.at(fld_type).trimmed().toInt(&ok); + int line_type = fields.at(fld_type).trimmed().toInt(&ok); if (!ok) { return; } - if (val_type != 1 && val_type != 5) { + if (line_type != line_sensors && line_type != line_gps) { return; } @@ -114,59 +101,42 @@ mynav_rd_line(const QString& line) } } - track_add_wpt(mynav_track, wpt); + track_add_wpt(track, wpt); } +/*************************************************************************** + * entry points called by gpsbabel main process * + ***************************************************************************/ -//*************************************************************************** -// global callbacks called by gpsbabel main process -//*************************************************************************** - -static void -mynav_rd_init(const QString& fname) +void +MyNavFormat::rd_init(const QString& fname) { - fin = gbfopen(fname, "rb", MYNAME); - mynav_track = route_head_alloc(); - track_add_head(mynav_track); + read_fname = fname; } -static void -mynav_rd_deinit() +void +MyNavFormat::rd_deinit() { - gbfclose(fin); + read_fname.clear(); } -static void -mynav_rd() +void +MyNavFormat::read() { - QString buff; + gpsbabel::TextStream stream; + stream.open(read_fname, QIODevice::ReadOnly, "mynav"); + + route_head* track = route_head_alloc(); + track_add_head(track); - while ((buff = gbfgetstr(fin)), !buff.isNull()) { - buff = buff.trimmed(); - if ((buff.isEmpty()) || (buff[0] == '#')) { + QString buf; + while (stream.readLineInto(&buf)) { + buf = buf.trimmed(); + if ((buf.isEmpty()) || buf.startsWith('#')) { continue; } - mynav_rd_line(buff); + read_line(buf, track); } -} -ff_vecs_t mynav_vecs = { - ff_type_file, - { - ff_cap_none, // waypoints - ff_cap_read, // tracks - ff_cap_none // routes - }, - mynav_rd_init, // rd_init - nullptr, // wr_init - mynav_rd_deinit, // rd_deinit - nullptr, // wr_deinit - mynav_rd, // read - nullptr, // write - nullptr, // exit - nullptr, //args - CET_CHARSET_ASCII, 0 //encode,fixed_encode - //NULL //name dynamic/internal? - , NULL_POS_OPS, - nullptr -}; + stream.close(); +} diff --git a/mynav.h b/mynav.h new file mode 100644 index 000000000..7276fe4c6 --- /dev/null +++ b/mynav.h @@ -0,0 +1,101 @@ +/* + Handle MyNav TRC format .trc and .ftn files + + Copyright (c) 2014-2020 Ralf Horstmann + Copyright (C) 2014-2020 Robert Lipe, robertlipe+source@gpsbabel.org + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + */ + +#ifndef MYNAV_H_INCLUDED_ +#define MYNAV_H_INCLUDED_ + +#include +#include + +#include "defs.h" +#include "format.h" + +class MyNavFormat : public Format +{ +public: + ff_type get_type() const override + { + return ff_type_file; + } + + QVector get_cap() const override + { + return { + ff_cap_none, // waypoints + ff_cap_read, // tracks + ff_cap_none // routes + }; + } + + QString get_encode() const override + { + return CET_CHARSET_ASCII; + } + + int get_fixed_encode() const override + { + return 0; + } + + void rd_init(const QString& fname) override; + void read() override; + void rd_deinit() override; + +private: + enum field_e { + fld_type = 0, + fld_lon, + fld_lat, + fld_direction, + fld_speed, + fld_altitude, + fld_timestamp, + fld_duration, + fld_gps_valid, + fld_distance, + fld_ascent, + fld_cadence, + fld_heart_rate, + fld_id, + fld_total_duration, + fld_terminator + }; + + enum line_e { + line_header = 0, + line_sensors = 1, + line_geonote = 2, + line_gps = 5, + line_lap_pause = 7, + line_lap_restart = 8, + line_total = 9, + line_lap_start = 10, + line_lap_end = 11, + }; + + static void read_line(const QString& line, route_head* track); + + QString read_fname; + +}; + +#endif diff --git a/vecs.h b/vecs.h index 6bb20cd57..1b9401d53 100644 --- a/vecs.h +++ b/vecs.h @@ -31,6 +31,7 @@ #include "ggv_bin.h" #include "gpx.h" #include "legacyformat.h" +#include "mynav.h" #if CSVFMTS_ENABLED @@ -173,7 +174,6 @@ extern ff_vecs_t mapbar_track_vecs; extern ff_vecs_t f90g_track_vecs; extern ff_vecs_t mapfactor_vecs; extern ff_vecs_t energympro_vecs; -extern ff_vecs_t mynav_vecs; extern ff_vecs_t geojson_vecs; extern ff_vecs_t globalsat_sport_vecs; #endif // MAXIMAL_ENABLED @@ -378,7 +378,7 @@ private: LegacyFormat f90g_track_fmt {f90g_track_vecs}; LegacyFormat mapfactor_fmt {mapfactor_vecs}; LegacyFormat energympro_fmt {energympro_vecs}; - LegacyFormat mynav_fmt {mynav_vecs}; + MyNavFormat mynav_fmt; LegacyFormat geojson_fmt {geojson_vecs}; GgvBinFormat ggv_bin_fmt; LegacyFormat globalsat_sport_fmt {globalsat_sport_vecs}; -- 2.30.2